package com.yoyo.yyadsdk.util;

import android.content.Context;

import com.anye.greendao.gen.LocalAdvEntityDao;
import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.bean.dao.LocalAdvEntity;
import com.yoyo.yyadsdk.bean.dao.RuleListEntity;
import com.yoyo.yyadsdk.config.PullFeature;

import java.util.ArrayList;
import java.util.List;

public class AdvUtils {

    public static ArrayList<AdvScreenEntity> getLockScreenList(@PullFeature.AdvTypeFlags int actionType) {
        ArrayList<AdvScreenEntity> res = new ArrayList<>();
        AdvType advType = new AdvType(actionType).invoke();
        if (advType.is()) return null;
        List<AdvScreenEntity> advScreenEntities = advType.getAdvScreenEntities();
        List<LocalAdvEntity> localAdvEntityList = advType.getLocalAdvEntityList();
        List<RuleListEntity> ruleListEntityList = advType.getRuleListEntityList();
        for (AdvScreenEntity p : advScreenEntities) {
            //校验是否符合展示条件
            if (checkAdv(localAdvEntityList, ruleListEntityList, p)) continue;
            res.add(p);
        }
        return res;
    }

    public static AdvScreenEntity getLockScreen(@PullFeature.AdvTypeFlags Integer... actionType) {
        AdvType advType = new AdvType(actionType).invoke();
        if (advType.is()) return null;
        List<AdvScreenEntity> advScreenEntities = advType.getAdvScreenEntities();
        List<LocalAdvEntity> localAdvEntityList = advType.getLocalAdvEntityList();
        List<RuleListEntity> ruleListEntityList = advType.getRuleListEntityList();
        for (AdvScreenEntity p : advScreenEntities) {
            if (checkAdv(localAdvEntityList, ruleListEntityList, p)) continue;
            return p;
        }
        return null;
    }

    private static boolean checkAdv(List<LocalAdvEntity> localAdvEntityList, List<RuleListEntity> ruleListEntityList, AdvScreenEntity p) {
        if (localAdvEntityList != null && localAdvEntityList.size() > 0) {
            LocalAdvEntity l = null;
            for (LocalAdvEntity localPullEntity : localAdvEntityList) {
                if (p.getId() == localPullEntity.getAdvId()) {
                    l = localPullEntity;
                    break;
                }
            }
            if (l != null) {
                //判断距上次展示间隔
                if (p.getInterval() * TimeUtils.SECOND >= System.currentTimeMillis() - l.getLastShowTime()) {
                    return true;
                }
                //判断周期内的展示次数
                if (p.getFrequency() <= l.getFrequencyCount()) {
                    return true;
                }
            }
        }
        //判断黑白名单
        RuleListEntity r = null;
        for (RuleListEntity ruleListEntity : ruleListEntityList)
            if (p.getRuleId() == ruleListEntity.getRuleId()) {
                r = ruleListEntity;
                break;
            }
        if (r != null) {
            if (r.getType() == PullFeature.PULL_WHITE_LIST) {
                //白名单
                return !r.getInWhiteRule();
            } else {
                //黑名单
                return r.getInBlackRule();
            }
        }
        return false;
    }

    /**
     * 广告记录本地
     *
     * @param advId   广告id
     * @param advType 广告类型
     */
    public static void recordAdvExposed(int advId, int advType) {
        LocalAdvEntityDao localAdvEntityDao = GreenDaoManager.getInstance().getDaoSession().getLocalAdvEntityDao();
        List<LocalAdvEntity> localAdvEntityList = localAdvEntityDao.queryBuilder().where(LocalAdvEntityDao.Properties.AdvId.in(advId)).list();
        LocalAdvEntity localAdvEntity;
        long frequencyId = SpUtil.getAdFrequencyId();
        if (localAdvEntityList != null && localAdvEntityList.size() > 0) {
            localAdvEntity = localAdvEntityList.get(0);
            if (localAdvEntity.getFrequencyId() < frequencyId) {
                //不在周期内
                localAdvEntity.setFrequencyCount(1);
            } else {
                //在周期内
                localAdvEntity.setFrequencyCount(localAdvEntity.getFrequencyCount() + 1);
            }
        } else {
            localAdvEntity = new LocalAdvEntity();
            localAdvEntity.setFrequencyCount(1);
        }
        localAdvEntity.setAdvId(advId);
        localAdvEntity.setLastShowTime(System.currentTimeMillis());
        localAdvEntity.setType(advType);
        localAdvEntity.setFrequencyId(frequencyId);
        localAdvEntityDao.insertOrReplaceInTx(localAdvEntity);
    }
}
